﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Barbar.UrlGuard.Dal.Persist;
using Barbar.UrlGuard.Dal;
using Barbar.UrlGuard.BL;

namespace Barbar.UrlGuard.Controls {
  public partial class ConfigurationAlerts : UserControl, IInjector<DataContext> {
    public event EventHandler ValuesApplied;

    public ConfigurationAlerts() {
      InitializeComponent();
    }

    private void chxSendEmail_CheckedChanged(object sender, EventArgs e) {
      emailSettings.Enabled = chxSendEmail.Checked;
    }

    //private void btnApply_Click(object sender, EventArgs e) {
    //  if (emailSettings.DoValidate()) {
    //    SaveValues();
    //    DataHolder.Instance.Persist();
    //    if (ValuesApplied != null)
    //      ValuesApplied(this, EventArgs.Empty);
    //  }
    //}

    //private void btnCancel_Click(object sender, EventArgs e) {
    //  LoadValues();
    //  emailSettings.DoValidate();
    //  if (ValuesApplied != null)
    //    ValuesApplied(this, EventArgs.Empty);
    //}

    //private void ConfigurationControl_Load(object sender, EventArgs e) {
    //  if (!DesignMode) {
    //    LoadValues();
    //  }
    //}

    #region IInjector<DataContext> Members

    public void SaveData(DataContext instance)
    {
      if (instance == null)
        throw new ArgumentNullException("instance");

      instance.UseBubbleAlert = chxShowBubble.Checked;
      if (chxSendEmail.Checked)
      {
        instance.EmailAlert = emailSettings.GetEmailAlert();
      }
      else
      {
        instance.EmailAlert = null;
      }
    }

    #endregion

    #region IReadOnlyInjector<DataContext> Members

    public void LoadData(DataContext readOnlyInstance)
    {
      if (readOnlyInstance == null)
      {
        chxShowBubble.Checked = true;
        chxSendEmail.Checked = false;
        emailSettings.Enabled = false;
      }
      else
      {
        chxShowBubble.Checked = readOnlyInstance.UseBubbleAlert;
        chxSendEmail.Checked = readOnlyInstance.EmailAlert != null;
        emailSettings.Enabled = readOnlyInstance.EmailAlert != null;
        emailSettings.SetEmailAlert(readOnlyInstance.EmailAlert);
      }
    }

    #endregion
  }
}
